package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign.question;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroup;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesFavoriteRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRelQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin
public class HomeworkQuestionMyCountController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesFavoriteRepository homeworkQuesFavoriteRepository;

    @Autowired
    private HomeworkQuesGroupRelQuesRepository homeworkQuesGroupRelQuesRepository;

    @Autowired
    private HomeworkQuesGroupRepository homeworkQuesGroupRepository;

    @ApiOperation(value = "作业系统老师试题数量")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/workassign/question/my/count", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String userId = profile.getId();
        long countFavorite = homeworkQuesFavoriteRepository.countByTeacherIdAndStageAndSubject(userId, reqModel.phase,
                reqModel.subject);
        RespModel model = new RespModel();
        model.favoriteCount = countFavorite;
        long myUpload = homeworkQuesGroupRelQuesRepository.countByGroupIdAndStageAndSubjectAndSchoolIdAndTeacherId(
                "myques", reqModel.phase, reqModel.subject, schoolId, userId);
        model.uploadCount = myUpload;

        List<GroupModel> groupModels = new ArrayList<HomeworkQuestionMyCountController.GroupModel>();
        List<HomeworkQuesGroup> homeworkQuesGroups = homeworkQuesGroupRepository
                .findBySchoolIdAndTeacherIdAndStageAndSubject(schoolId, userId, reqModel.phase, reqModel.subject);
        for (HomeworkQuesGroup homeworkQuesGroup : homeworkQuesGroups) {
            GroupModel groupModel = new GroupModel();
            groupModel.id = homeworkQuesGroup.getId();
            groupModel.quesCount = homeworkQuesGroupRelQuesRepository.countByGroupId(groupModel.id);
            groupModels.add(groupModel);
        }
        model.groupModels = groupModels;
        // 获取
        return model;
    }

    protected static class ReqModel {
        public String phase;
        public String subject;
    }

    protected static class GroupModel {
        public String id;
        public long quesCount;
    }

    protected static class RespModel {
        public List<GroupModel> groupModels;
        // 收藏试题数量
        public long favoriteCount;

        // 我的上传试题数量（默认分组，并不是全部自己上传的）
        public long uploadCount;
    }

}
